package com.example.service.impl;


import com.example.dto.RegisterDto;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param userId 用户ID
     * @param password 用户密码
     * @return 登录成功的用户对象
     * @throws AccountNotFoundException 账号不存在异常
     * @throws IncorrectPasswordException 密码错误异常
     */
    @Override
    @Transactional
    public User login(String userId, String password) {
        User user = userMapper.selectByUserId(userId);
        if (user == null) {
            throw new AccountNotFoundException("账号不存在");
        } else if (!user.getPassword().equals(password)) {
            throw new IncorrectPasswordException("密码错误");
        } else {
            return user;
        }
    }

    /**
     * 账号不存在异常
     */
    @Transactional
    public class AccountNotFoundException extends RuntimeException {
        public AccountNotFoundException(String message) {
            super(message);
        }
    }
    /**
     * 密码不存在异常
     */
    @Transactional
    public class IncorrectPasswordException extends RuntimeException {
        public IncorrectPasswordException(String message) {
            super(message);
        }
    }



    /**
     * 用户注册
     * @param registerDto 包含注册信息的数据传输对象
     * @return 注册成功的用户对象
     */
    @Override
    @Transactional
    public User register(RegisterDto registerDto) {
        // Check if userId already exists
        User existingUser = userMapper.selectOrderByUserId(registerDto.getUserId());
        if (existingUser != null) {
            throw new IllegalArgumentException("账户已存在");
        }

        // Validate and process registration
        if (registerDto.getUserId() == null) {
            throw new IllegalArgumentException("Phone number cannot be empty.");
        }
        if (registerDto.getPassword() == null) {
            throw new IllegalArgumentException("Password cannot be empty.");
        }
        if (registerDto.getRealName() == null) {
            throw new IllegalArgumentException("Real name cannot be empty.");
        }
        if (registerDto.getBirthday() == null) {
            throw new IllegalArgumentException("Birthday cannot be empty.");
        }
        if (registerDto.getSex() == null) {
            throw new IllegalArgumentException("Gender cannot be empty.");
        }
        if (registerDto.getIdentityCard() == null) {
            throw new IllegalArgumentException("Identity card number cannot be empty.");
        }
        if (registerDto.getUserType() == null) {
            throw new IllegalArgumentException("User type cannot be empty.");
        }

        // Convert frontend date string to Date type, assuming birthday format is "yyyy-MM-dd"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = null;
        try {
            birthday = sdf.parse(registerDto.getBirthday());
        } catch (ParseException e) {
            throw new RuntimeException("Invalid date format", e);
        }

        // Create User object from DTO data
        User user = new User();
        user.setUserId(registerDto.getUserId());
        user.setPassword(registerDto.getPassword());
        user.setRealName(registerDto.getRealName());
        user.setSex(registerDto.getSex());
        user.setIdentityCard(registerDto.getIdentityCard());
        user.setBirthday(birthday);
        user.setUserType(registerDto.getUserType());

        // Save user to database
        userMapper.insert(user);

        return user;
    }

    @Override
    @Transactional
    public Integer getUserGenderByUserId(String userId) {
        return userMapper.getSexByUserId(userId);
    }

    @Override
    @Transactional
    public User selectOrderByUserId(String userId) {
        return userMapper.selectOrderByUserId(userId);
    }

    @Override
    public Integer getUserSexByUserId(String userId) {
        return 0;
    }
}
